#include "widget.h"
#include "config.h"
#include <QIcon>
#include <QPainter>
#include <QMouseEvent>
#include <ctime>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    initscene();
    playGame();
}

Widget::~Widget()
{}

void Widget::initscene()
{
    setFixedSize(gamewidth,gameheight);
    setWindowTitle(title);
    setWindowIcon(QIcon(gameicon));
    m_Timer.setInterval(gamerate);
    m_recorder=0;
    srand((unsigned int)time(NULL));
}

void Widget::playGame()
{
    m_Timer.start();
    connect(&m_Timer,&QTimer::timeout,[=](){
        enemyToscene();
        updatePosition();
        collisionDetection();
        update();
    });
}

void Widget::updatePosition()
{
    m_map.mapPosition();
    m_hero.shoot();
    for(int i=0;i<BULLET_NUM;i++)
    {
        if(m_hero.m_bullets[i].m_Free==false)
        {
            m_hero.m_bullets[i].updatePosition();
        }
    }
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free==false)
        {
            m_enemys[i].updatePosition();
        }
    }
    for(int i=0;i<BOMBNUM;i++)
    {
        if(m_bombs[i].m_Free==false)
        {
            m_bombs[i].updateInfo();
        }
    }
}

void Widget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);
    painter.drawPixmap(m_hero.m_x,m_hero.m_y,m_hero.m_plane);
    for(int i=0;i<BULLET_NUM;i++)
    {
        if(m_hero.m_bullets[i].m_Free==false)
        {
            painter.drawPixmap(m_hero.m_bullets[i].m_x,m_hero.m_bullets[i].m_y,m_hero.m_bullets[i].m_Bullet);
        }
    }
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free==false)
        {
            painter.drawPixmap(m_enemys[i].m_x,m_enemys[i].m_y,m_enemys[i].m_enemy);
        }
    }
    for(int i=0;i<BOMBNUM;i++)
    {
        if(m_bombs[i].m_Free==false)
        {
            painter.drawPixmap(m_bombs[i].m_x,m_bombs[i].m_y,m_bombs[i].m_pixArr[m_bombs[i].m_index]);
        }
    }
}


void Widget::mouseMoveEvent(QMouseEvent * event)
{
    int x=event->x()-m_hero.m_Rect.width()*0.5;
    int y=event->y()-m_hero.m_Rect.height()*0.5;
    if(x<=0)
    {
        x=0;
    }
    if(x>=gamewidth-m_hero.m_Rect.width())
    {
        x=gamewidth-m_hero.m_Rect.width();
    }
    if(y<=0)
    {
        y=0;
    }
    if(y>=gameheight-m_hero.m_Rect.height())
    {
        y=gameheight-m_hero.m_Rect.height();
    }
    m_hero.setPosition(x,y);
}

void Widget::enemyToscene()
{
    m_recorder++;
    if(m_recorder<ENEMY_INTERVAL)
    {
        return;
    }
    m_recorder=0;
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            m_enemys[i].m_Free=false;
            m_enemys[i].m_x=rand()%(gamewidth-m_enemys[i].m_Rect.width());
            m_enemys[i].m_y=-m_enemys[i].m_Rect.height();
            break;
        }
    }
}

void Widget::collisionDetection()
{
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            continue;
        }

        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_hero.m_bullets[i].m_Free)
            {
                continue;
            }

            if(m_enemys[i].m_Rect.intersects(m_hero.m_bullets[j].m_Rect))
            {
                m_enemys[i].m_Free=true;
                m_hero.m_bullets[j].m_Free=true;
                for(int k=0;k<BOMBNUM;k++)
                {
                    if(m_bombs[k].m_Free)
                    {
                        m_bombs[k].m_Free=false;
                        m_bombs[k].m_x=m_enemys[i].m_x;
                        m_bombs[k].m_y=m_enemys[i].m_y;
                        break;
                    }
                }
            }
        }
    }
}




